#bilibili排名算法 v3.0(2012.2.20)
#推荐评分计入
#按类别双算法
class Results::R30 < Results::Base
    #评论基础倍值
    E_C = 50
   #收藏基础倍值
    E_S = 100
   #评分基础倍值
    E_T = 0.2

  @space_fix = nil

  def self.version;self.to_s.split('::R').last.to_f / 10;end

  def get_ranking_point
    (work.is_in_rank? ? point_normal : point_bangumi).to_i
  end

  def self.get_stow_revised stows
    if stows <= 300
      E_S
    else
      fixed = (E_S*300 + 20*(stows - 300))/stows.to_f
      format("%.2f", fixed).to_f
    end
  end
  def stow_revised;self.class.get_stow_revised(stows);end

  def self.get_comment_revised clicks, comments, stows, tj
    return E_C.to_f if comments <= 0
    [format("%.2f", E_C * (stows * E_C + tj) / (clicks.to_f + tj + comments.to_f * E_C)).to_f, E_C.to_f].min
  end
  def self.get_comment_revised_bangumi clicks, comments
    return E_C.to_f if comments <= 0
    format("%.2f", E_C * clicks.to_f / (clicks.to_f + comments.to_f * E_C)).to_f
  end
  def comment_revised;self.class.get_comment_revised clicks, comments, stows, tj;end
  def comment_revised_bangumi;self.class.get_comment_revised_bangumi clicks, comments;end

  def self.get_tj_revised
    E_T
  end
  def tj_revised;self.class.get_tj_revised;end

  def self.get_click_revised clicks, parts
    revised1 = format("%.3f", 2.0 / (parts + 1)).to_f
    point1 = clicks * revised1
    if point1 <= 50000
      revised1
    else
      revised2 = (50000 + 0.5*(point1 - 50000))/point1.to_f
      fixed = revised1 * format("%.3f", revised2).to_f
      format("%.3f", fixed).to_f
    end
  end
  def click_revised;self.class.get_click_revised(clicks, work.part_count);end

  def click_point;clicks * click_revised;end
  def comm_point;comments * comment_revised;end
  def comm_point_bangumi;comments * comment_revised_bangumi;end
  def stow_point;stows * stow_revised;end
  def tj_point;tj * tj_revised;end

  def space_fix
    return 1 unless work.is_in_rank?
    return @space_fix if @space_fix
    if @@space_fix_cache
      unless @space_fix = @@space_fix_cache[work.wid]
        if ([19, 20, 21, 22].include? work.ac_type_id) && !work.youku_and_tudou.empty?
          @space_fix = (@@space_fix_cache[work.wid] = 0.5)
        else
          @space_fix = (@@space_fix_cache[work.wid] = 1)
        end
      end
    else
      if ([19, 20, 21, 22].include? work.ac_type_id) && !work.youku_and_tudou.empty?
        @space_fix = 0.5
      else
        @space_fix = 1
      end
    end
    @space_fix
  end

  def is_hot_bangumi?
    return self[:hot_bangumi] unless self[:hot_bangumi].nil?
    if work.ac_type_id == 25 && clicks > 20000
      bw = BangumiWork.first :include => :bangumi, :conditions => ["work_id = ? and bangumis.status = 1", work_id]
      self[:hot_bangumi] = bw ? true : false
    else
      self[:hot_bangumi] = false
    end
  end

private
  def point_normal
    (click_point + comm_point + stow_point + tj_point) * space_fix
  end

  def point_bangumi
    click_point + comm_point_bangumi
  end
  
end
